package com.gc.web.controller.business;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.gc.business.domain.AdminRegion;
import com.gc.business.service.IAdminRegionService;
import com.gc.common.base.Result;
import com.gc.common.support.Convert;
import com.gc.common.utils.IdUtils;
import com.gc.common.utils.StringUtils;
import com.gc.framework.util.ShiroUtils;
import com.gc.system.domain.SysDept;
import com.gc.system.domain.SysRole;
import com.gc.system.service.ISysDeptService;
import com.gc.system.service.ISysUserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.CompanyManage;
import com.gc.business.service.ICompanyManageService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 公司管理 控制层
 * 
 * @author hruiyan
 * @date 2020-11-12
 */
@Slf4j
@Controller
@RequestMapping("/business/companyManage")
public class CompanyManageController extends BaseController {
    private String prefix = "business/companyManage";
	
	private final ICompanyManageService companyManageService;
	private final IAdminRegionService adminRegionService;
	private final ISysUserService userService;
	private final ISysDeptService deptService;

	@Autowired
	public CompanyManageController(ICompanyManageService companyManageService,
								   IAdminRegionService adminRegionService,
								   ISysUserService userService,
								   ISysDeptService deptService) {
		this.companyManageService = companyManageService;
		this.adminRegionService = adminRegionService;
		this.userService = userService;
		this.deptService = deptService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:companyManage:view")
	@GetMapping
	public String companyManage(ModelMap mmap){

		Long attrId = getAttrId();
		//无归属ID，为平台管理人员 -- 查询全部权限
		if(StringUtils.isNull(attrId)){
			attrId = 0L;
		}
		mmap.put("attrId",attrId);
		return prefix + "/companyManage";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap){
		List<AdminRegion> regionList = adminRegionService.selectAdminRegion();
		mmap.put("regionList",regionList);
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap){
		CompanyManage companyManage = companyManageService.getById(id);
		List<AdminRegion> regionList = adminRegionService.selectAdminRegion();
		mmap.put("regionList",regionList);
		mmap.put("companyManage", companyManage);
		return prefix + "/edit";
	}
	
	/**
	 * 查询公司管理列表
	 */
	@RequiresPermissions("business:companyManage:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(CompanyManage companyManage){
		List<CompanyManage> list = null;
		startPage();
		if(StringUtils.isNull(getAttrId())){
			list = companyManageService.list(companyManage);
		}else{
			list = companyManageService.selectCompanyManageByGovId(getAttrId(),companyManage.getRegionId());
		}
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存公司管理
	 */
	@RequiresPermissions("business:companyManage:add")
	@Log(title = "公司管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public Result addSave(CompanyManage companyManage){
		companyManage.setCompanyId(IdUtils.getPrimaryKey());
		companyManage.setCreateTime(new Date());
		companyManage.setCreateBy(ShiroUtils.getLoginName());
		companyManageService.save(companyManage);

		//保存父节点部门
		SysDept dept = new SysDept();
		dept.setDeptName(companyManage.getCompanyName());
		dept.setAttrId(companyManage.getCompanyId());
		dept.setParentId(0L);
		deptService.save(dept);
		return Result.success();
	}

	/**
	 * 修改保存公司管理
	 */
	@RequiresPermissions("business:companyManage:edit")
	@Log(title = "公司管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(CompanyManage companyManage){
		companyManageService.updateById(companyManage);
		return Result.success();
	}
	
	/**
	 * 删除公司管理
	 */
	@RequiresPermissions("business:companyManage:remove")
	@Log(title = "公司管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		companyManageService.removeByIds(Arrays.asList(longIds));
		for (Long attrId : longIds) {
			deptService.deleteDeptByAttrId(attrId);
		}
		return Result.success();
	}

	/**
	 * 导出公司管理列表
	 */
	@RequiresPermissions("business:companyManage:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(CompanyManage companyManage){
		List<CompanyManage> list = companyManageService.list(companyManage);
		ExcelUtil<CompanyManage> util = new ExcelUtil<>(CompanyManage.class);
		util.exportExcel(list, "companyManage");
		return Result.success();
	}
}
